package com.lab.webSocket.utils;/*
package com.lab.webSocket.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lab.webSocket.dto.UserDto;
import com.lab.webSocket.service.LoginService;
import com.lab.webSocket.service.impl.LoginServiceImpl;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

*
 * Created by xu_jiaHui on 2017/8/25.


@ServerEndpoint("/connection/{userId}/{userName}")
public class WebSocketTest {
    //静态变量 记录当前在线连接数
    private static int onlineCount=0;
    //concurrent包的安全set，用来存放每个客户端对应的MyWebSocket对象。
    private static CopyOnWriteArraySet<WebSocketTest> webSocketSet=new CopyOnWriteArraySet<WebSocketTest>();

    //与某个客户端的连接会话， 需要通过它来给客户端发送数据
    private Session session;
    private String userName;
    private String userId;
    private static List list = new ArrayList<String>();   //在线列表,记录用户名称
    private static Map session4userId=new HashMap();


    public  static synchronized int getOnlineCount() {
        return onlineCount;
    }
    private  static synchronized void addOnlineCount() {
        WebSocketTest.onlineCount++;
    }
    private  static synchronized void subOnlineCount() {
        WebSocketTest.onlineCount--;
    }

*
     * @Description: 连接建立成功时调用方法
     * @author xu_jiaHui
     * @date 2017/8/25 15:45


    @OnOpen
    public void onOpen(@PathParam("userId")String userId,
                       @PathParam("userName") String userName, Session session){

        this.userId=userId;
        this.userName=userName;
        this.session=session;
        webSocketSet.add(this);//加入Set中
        addOnlineCount();//在线数+1
        list.add(userId);
        session4userId.put(userId,session);
        String message = getMessage("[" + userName + "]加入聊天室,当前在线人数为"+getOnlineCount()+"位", "notice",  list);
        broadcast(message);
        System.out.println("有新连接加入，当前连接数为："+getOnlineCount());
    }

*
     * @Description: 连接断开时调用方法
     * @author xu_jiaHui
     * @date 2017/8/25 15:45


    @OnClose
    public void onClose(){
        webSocketSet.remove(this);
        subOnlineCount();
        list.remove(this.userId);
        session4userId.remove(this.userId);
        String message = getMessage("[" + this.userName +"]离开了聊天室,当前在线人数为"+getOnlineCount()+"位", "notice", list);
        broadcast(message);         //广播
        System.out.println("有连接断开，当前连接数为："+getOnlineCount());
    }


*
     * @Description: 接受客户端的message，判别个人还是广播
     * @author xu_jiaHui
     * @date 2017/8/25 19:10


    @OnMessage
    public void onMessage(String _message) {
        JSONObject chat = JSON.parseObject(_message);
        JSONObject message = JSON.parseObject(chat.get("message").toString());
        if(message.get("to") == null || message.get("to").equals("")){      //如果to为空,则广播;如果不为空,则对指定的用户发送消息
            broadcast(message.get("content").toString());
        }else{
            String [] userList = message.get("to").toString().split(",");
            String content=message.get("content").toString();
            singleSend(content, (Session) session4userId.get(message.get("from")));      //发送给自己
            for(String user : userList){
                if(!user.equals(message.get("from"))){
                    singleSend(content, (Session) session4userId.get(user));     //分别发送给每个指定用户
                }
            }
        }
    }

*
     * @Description: 广播消息
     * @author xu_jiaHui
     * @date 2017/8/25 15:48


    public void broadcast(String message){
        System.out.println("来自客户端消息："+message);
        for(WebSocketTest item: webSocketSet){
            try{
                item.session.getBasicRemote().sendText(message);
            }catch (Exception e){
                e.printStackTrace();
                continue;
            }
        }
    }

*
     * @Description: 私聊
     * @author xu_jiaHui
     * @date 2017/8/25 19:14


    public void singleSend(String message, Session session){
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
*
     * @Description: 发生错误时报错
     * @author xu_jiaHui
     * @date 2017/8/25 15:51


    @OnError
    public void onError(Session session,Throwable throwable){
        System.out.println("发生错误！！！");
        throwable.printStackTrace();
    }

*
     * @Descrption: 组装返回的json字符串
     * @author xu_jiaHui
     * @date 2017/8/25 19:16


    public String getMessage(String message, String type, List list){
        JSONObject member = new JSONObject();
        member.put("message", message);
        member.put("type", type);
        member.put("list", list);
        return member.toString();
    }


}
*/
